package com.ywdnf.gomoku.model.chess;

import com.ywdnf.gomoku.util.Const;
import com.ywdnf.gomoku.util.IdUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;
import lombok.Getter;
import lombok.Setter;
import org.springframework.util.StringUtils;

/**
 * 玩家模型
 *
 * @author lss
 * @date 2019-06-25, Tue
 */
@Getter
@Setter
public class Player {


  private static Random r = new Random();

  /**
   * 玩家id
   */
  private String userId;

  /**
   * 当前所处游戏id
   */
  private String gameId;

  /**
   * 昵称
   */
  private String nick;

  /**
   * 是否创建者
   */
  private Role role;

  /**
   * 用户积分
   */
  private int integral;

  /**
   * 当前玩家下过的棋子
   */
  private List<Point> myPoints = new ArrayList<>();

  public Player(String gameId, String nick, Role role) {
    this.userId = IdUtils.specifyPrefixAndLengthId(Const.USER_ID_PREFIX, null, Const.USER_ID_LEN_WITHOUT_PREFIX);
    this.gameId = gameId;
    this.nick =
        StringUtils.isEmpty(nick) ? Const.NICK_ARR[r.nextInt(Const.NICK_ARR.length)] : nick;
    this.role = role;
  }

  @Override
  public boolean equals(Object obj) {
    if (!(obj instanceof Player)) {
      return false;
    }
    Player p = (Player) obj;
    return p.getUserId().equals(userId) && p.getGameId().equals(gameId);
  }

  @Override
  public int hashCode() {
    return Objects.hashCode(this);
  }

  public void winAndIncreaseIntegral() {
    this.integral += Const.WIN_AWARD_INTEGRAL;
  }
}
